package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import entity.Idea;
import entity.User;
import service.UserService;
import util.db.DBhelp;
import util.db.RowMapper;
import util.web.Page;

public class IdeaDao {

	private DBhelp<Idea> dBhelp = new DBhelp<Idea>();
	
	private class IdeaRowMapper implements RowMapper<Idea> {

		public Idea mapRow(ResultSet resultSet) throws SQLException {
			Idea idea = new Idea();
			idea.setId(resultSet.getString("id"));
			idea.setIdeatitle(resultSet.getString("ideatitle"));
			idea.setContent(resultSet.getString("content"));
			idea.setCreatetime(resultSet.getString("createtime"));
			idea.setUserid(resultSet.getString("userid"));
			idea.setProjectid(resultSet.getString("projectid"));
			return idea;
		}
		
	}

	public void save(Idea idea) {
		String sql = "insert into t_idea (id,ideatitle,content,createtime,userid,projectid)values(?,?,?,?,?,?)";
		dBhelp.update(sql, idea.getId(),idea.getIdeatitle(),idea.getContent(),idea.getCreatetime(),idea.getUserid(),idea.getProjectid());
		
	}

	public Page<Idea> findIdeaByProjectidLimit(int nowPageNum, String id) {
		Page<Idea> page = new Page<Idea>(nowPageNum, countByProjectid(id));
		String sql = "select * from t_idea where projectid=? order by createtime desc limit ?,?";
		List<Idea> list = dBhelp.executeQueryForList(sql, new RowMapper<Idea>() {

			public Idea mapRow(ResultSet resultSet) throws SQLException {
				Idea idea = new Idea();
				idea.setId(resultSet.getString("id"));
				idea.setIdeatitle(resultSet.getString("ideatitle"));
				idea.setContent(resultSet.getString("content"));
				idea.setCreatetime(resultSet.getString("createtime"));
				idea.setUserid(resultSet.getString("userid"));
				idea.setProjectid(resultSet.getString("projectid"));
				
				idea.setUser(new UserService().findUserById(resultSet.getString("userid")));
				return idea;
			}
		}, id,page.getStartPage(),page.getPagesize());
		page.setList(list);
		return page;
	}

	private int countByProjectid(String id) {
		String sql = "select count(*) from t_idea where projectid=?";
		return dBhelp.executeQuery(sql, id).intValue();
	}

	public Idea findIdeaById(String id) {
		String sql = "select * from t_idea where id=?";
		return dBhelp.executeQueryToObject(sql, new RowMapper<Idea>() {

			public Idea mapRow(ResultSet resultSet) throws SQLException {
				Idea idea = new Idea();
				idea.setId(resultSet.getString("id"));
				idea.setIdeatitle(resultSet.getString("ideatitle"));
				idea.setContent(resultSet.getString("content"));
				idea.setCreatetime(resultSet.getString("createtime"));
				idea.setUserid(resultSet.getString("userid"));
				idea.setProjectid(resultSet.getString("projectid"));
				idea.setUser(new UserService().findUserById(resultSet.getString("userid")));
				return idea;
			}
		}, id);
	}

	public void removeById(String id) {
		String sql = "delete from t_idea where id=?";
		dBhelp.update(sql, id);
	}
}
